package com.up.vms.application.service;

import com.github.pagehelper.PageInfo;
import com.up.vms.application.base.utils.IdCardUtils;
import com.up.vms.application.base.utils.JsonUtils;
import com.up.vms.application.common.utils.UnionUtils;
import com.up.vms.domain.module.SycUserInfo;
import com.up.vms.domain.op.entity.UserDO;
import com.up.vms.domain.op.repository.UserDORepository;
import com.up.vms.domain.vm.entity.Activity;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class UserService {

    private Log logger = LogFactory.getLog(getClass());

    @Autowired
    private UserDORepository userDORepository;

    /**
     * create By  LuJun
     * 根据统一接口更新生成用户信息
     *
     * @param userInfo
     * @return
     */
    public UserDO insertUser(SycUserInfo userInfo) throws Exception {
        int age = IdCardUtils.getAgeByIdCard(userInfo.getIdCardNum());
        //M-男，F-女，N-未知
        String userSex = IdCardUtils.getGenderByIdCard(userInfo.getIdCardNum());
        String sexName = "未知";
        if ("M".equalsIgnoreCase(userSex)) {
            sexName = "男";
        } else if ("F".equalsIgnoreCase(userSex)) {
            sexName = "女";
        }

        //先查询当前用户是否存在
        List<UserDO> userDOList = userDORepository.findALLByOpenId(userInfo.getOpenId());

        UserDO model = new UserDO();
        model.setUserName(userInfo.getRealName());
        model.setNickName(userInfo.getRealName());
        model.setIdNumber(userInfo.getIdCardNum());
        model.setUserPhone(userInfo.getPhoneNum());
        model.setCompName(userInfo.getCompany());
        model.setHeadImg(userInfo.getPhoto());
        String memberUnionCode = userInfo.getMemberUnionCode();
        String unionNo = UnionUtils.unionFormat(memberUnionCode);
        model.setUnionNo(unionNo);                      //所属工会
        model.setUserType(userInfo.getUserType());      //会员身份标识
        model.setOpenId(userInfo.getOpenId());

        if (userDOList == null || userDOList.size() <= 0) {
            model.setPassword("");
            model.setUserAge(age);
            model.setUserSex(sexName);
            model.setEmail("");
            model.setAddress("");
            model.setCreateTime(new Date());
            model.setCreator(1);
            model.setIsDeleted(1);
            //返回插入的数据的主键
            logger.debug("插入用户信息: " + JsonUtils.toStringNoEx(model));
            model.setInBlacklistTime(null);
            model.setInBlacklist(0);
            UserDO insert = userDORepository.save(model);
            return insert;
        } else {
            //用户已经保存，获得其主键
            Long id = userDOList.get(0).getId();
            model.setId(id);
            model.setUserAge(age);
            model.setUserSex(sexName);
//            //判断黑名单是否已经取消
//            if (model.getInBlacklist() != null && new Integer(1).equals(model.getInBlacklist())) {
//                if (model.getInBlacklistTime() != null && model.getInBlacklistTime().before(new Date())) {
//                    model.setInBlacklist(0);
//                    model.setInBlacklistTime(null);
//                }
//            }
            logger.debug("更新用户信息: " + JsonUtils.toStringNoEx(model));
            //根据openId更新
            UserDO update = userDORepository.save(model);
            return update;

        }
    }

    public List<UserDO> selectByOpenId(String openId) {
        List<UserDO> userDOList = userDORepository.findALLByOpenId(openId);
        return userDOList;
    }

    public UserDO getById(Long userId) {
        Optional<UserDO> optional = userDORepository.findById(userId);

        if (optional.isPresent()) {
            return optional.get();
        } else {
            return null;
        }
    }
}
